//
//  PopoverPresentationController.swift
//  WeiBo
//
//  Created by Gao on 16/3/5.
//  Copyright © 2016年 Gao. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController
{
    var presentFrame:CGRect = CGRectZero
    /**
     实例化 一个负责转场的控制器
     
     - parameter presentedViewController:  被展示的控制器
     - parameter presentingViewController: 发起的控制器
     
     - returns: 一个转场动画的控制器实例
     */
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController)
    {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    /**
     containerView  这个是容器视图  转场动画上展示的东西 都是放在他上面的
     presentedView  被展示的视图
     */
    override func containerViewWillLayoutSubviews()
    {
        //1.增加遮盖视图
        containerView?.insertSubview(coverView, atIndex: 0)
        
        //2.设置展示视图
        if presentFrame == CGRectZero
        {
            presentedView()?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }
        else
        {
            presentedView()?.frame = presentFrame
        }
    }
    /// 遮盖视图
    lazy var coverView:UIView =
    {
       let view = UIView()
           view.frame = UIScreen.mainScreen().bounds
           view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        let tap = UITapGestureRecognizer(target: self, action: "close")
           view.addGestureRecognizer(tap)
       return view
    }()
    
    func close()
    {
        self.presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
